﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TMScore_Subsets
{
    class DocumentsManager // handle all the documents, create the docs, make tfidf,send the docs for  the subset TMscore calculation
    {

        public List<Document> allDocuments { get; set; } // keeps all the documents in the repository

        public DocumentsManager()
        {
            allDocuments = new List<Document>();
        }

        public List<User> ReadFile()
        {
            
             string m_Name; 
             DateTime time; // the time the mail sent
             List<string> receivers;// List of receivers
             double TMScore; //TMscoure of the document
             Dictionary<int, int> Fingerprints;// the document fingerprints, i.e. hash value of some textual content. 
             UsersManager manager = new UsersManager();
            string [] t=null;
            FileStream file = new FileStream(@"info.txt", FileMode.Open);
            StreamReader sr = new StreamReader(file); // open a new StreamReader
            while (sr.EndOfStream!=true) // for each of the files
            {
                receivers = new List<string>(); 
                Fingerprints = new Dictionary<int, int>();
                int current = 0;
                string line = sr.ReadLine(); // Read a line 
                string[] split = line.Split(new char[] { ',' }); // split the line and get the Doc Number
                m_Name = split[0];
                t =split[1].Split(new char[] { '/' });
                time = new DateTime(Convert.ToInt32(t[2].Substring(0, 4)), Convert.ToInt32(t[1]), Convert.ToInt32(t[0]));
                TMScore = Double.Parse(split[2]);
                line = sr.ReadLine();
                split = line.Split(new char[] { ',' });
                foreach (string rec in split)
                {
                    if (current+1 < split.Length)
                    {
                    receivers.Add(rec);
                    current++;
                    }
                }
                line = sr.ReadLine();
                split = line.Split(new char[] { ',' });
                for (int i = 0; i < split.Length; i=2+i)
                {
                    if( i+1!=split.Length)
                    Fingerprints.Add(int.Parse(split[i]),int.Parse(split[i+1]));
                } // create in the end the document object, new user, and add the doc to the list
                Document d = new Document(m_Name, receivers, time, TMScore, Fingerprints);;//Dictionary<fileName ,tuple <all text, list of recivers,date,tmscore>
                manager.CreateUser(d); // create new users from the mail
                allDocuments.Add(d);  
            }
            return manager.usersList;
        }
       
    }
}
